package com.xxl.controller.impl;

import com.xxl.controller.AdminApi;
import com.xxl.dto.BlogDto;
import com.xxl.dto.HomeDto;
import com.xxl.dto.UserDto;
import com.xxl.service.BlogService;
import com.xxl.service.UserService;
import com.xxl.util.GlobalResult;
import com.xxl.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author xxl
 * @date 2023/4/7
 */
@RestController
@RequestMapping(value = "/admin",produces = "application/json")
public class AdminApiImpl implements AdminApi<Map<String,Object>> {
    @Resource
    BlogService blogService;
    @Resource
    JwtUtil jwtUtil;
    @Resource
    GlobalResult resultUtil;
    @Resource
    UserService userService;


    @Override
    @GetMapping("queryAllBlog")
    public Map<String, Object> queryAllBlog() {
        List<HomeDto> homeDtoList = blogService.queryAllBlog();
        return resultUtil.result(200,"查询成功",homeDtoList);
    }

    @Override
    @GetMapping("queryAllUser")
    public Map<String, Object> queryAllUser() {
        List<UserDto> userDtoList = userService.queryAllUser();
        return resultUtil.result(200,"查询成功",userDtoList);
    }

    @Override
    @PostMapping("modifyUserRole/{roleId}")
    public Map<String, Object> modifyUserRole(@PathVariable("roleId") int roleId,@RequestParam("username")String username)
    {
        String name = userService.findByUsername(username);
        if (name == null) {
            return resultUtil.result(500, "用户不存在");
        }

        return null;
    }

    @Override
    @PostMapping("deleteUserByName/{username}")
    public Map<String, Object> deleteUserByName(@PathVariable("username") String username) {
        String name = userService.findByUsername(username);
        if (name == null) {
            return resultUtil.result(500, "用户不存在");
        }
        List<BlogDto> list = blogService.queryAllBLogByUsername(username);
        for (BlogDto s : list) {
            //删除博客数据的
            blogService.deleteBlogByUUID(s.getBlogUuid());
        }
        //删除用户数据库
        int i = userService.deleteUserByUsername(username);
        if (i > 0) {
            return resultUtil.result(200,"删除成功");
        }
        return resultUtil.result(200,"删除失败");
    }

    @Override
    @PostMapping("deleteBlogByUUID/{uuid}")
    public Map<String, Object> deleteBlogByUuid(@PathVariable("uuid") String uuid) {
        int i = blogService.deleteBlogByUUID(uuid);
        if (i > 0) {
            return resultUtil.result(200,"删除成功");
        }
        return resultUtil.result(200,"删除失败");
    }

}
